/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aiaudio.processing.split.mappers;

import aiaudio.Application;
import java.io.IOException;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapper;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.BooleanWritable;

/**
 *
 * @author nastya
 */
public class CalculateNeededUserArtistMapper extends TableMapper<ImmutableBytesWritable, BooleanWritable> {

    public static final String TABLE_TO_CHECK_NAME_PARAM = "tableToCheck";
    HTable tableToCheck;

    @Override
    protected void map(ImmutableBytesWritable key, Result value, Context context) throws IOException, InterruptedException {
        String user = Bytes.toString(key.get());
        boolean exists = checkIfExists(user);
        context.write(new ImmutableBytesWritable(key.get()), new BooleanWritable(exists));
    }

    @Override
    protected void setup(Context context) throws IOException, InterruptedException {
        super.setup(context);
        String tableToCheckName = context.getConfiguration().get(TABLE_TO_CHECK_NAME_PARAM);
        tableToCheck = Application.database().getByName(tableToCheckName).getTable();
    }

    private boolean checkIfExists(String userName) throws IOException {
        Get get = new Get(Bytes.toBytes(userName));
        return tableToCheck.exists(get);
    }
}
